/**
* 作者 ：食草狂魔
*
* 日期 ：2017/11/24
*
* 描述 ：目录管理新建 - 关联部门
*/
<template>
  <div class="dialogContent">
  	<el-container class="is-horizontal classification">
  		<!--<p class="dialogBar"></p>-->
	    <el-aside>
	    	<TreeBar :options="treeOptions" class="catalogTree"></TreeBar>
	    </el-aside>
	    <el-main>
        <MTable :options="tableOptions" ref="myTable" class="tablebar"></MTable>
      </el-main>
   </el-container>
  </el-container>
  </div>
</template>
<script>
  import config from '../../../../config/config'
  import dirAPIs from '../../../../api/dirAPIs'
  import departmentAPIs from '../../../../api/departmentAPIs'
  import TreeBar from '../../../../components/treebar/TreeBar.vue'
  import MTable from '../../../../components/table/Table.vue'
 

  export default {
    name: 'main',
    components: {
    	TreeBar,
      MTable
    },
    data () {
      return {
        menuState: 'catalogManage',
        treeOptions: {
        	see: true,
        	data:[
        	],
        	treeMethod:{
        		append(node, data, store){

        		},
        		remove(node, data, store){

        		},
            nodeClick () {}
        	}
        },
        message: "管理",
        tableOptions: {
        	handleSelectionChange (rows) {

          }
        },
        createTimeToStart: +new Date("1971/01/01 00:00:00"),
        createTimeToEnd: +new Date(),
        catalogManageModel: this,
        addCatalogTypeFormData: {},
        dirId: "", // 全局选择目录分类ID
        dirTypeId: "", // 目录分类ID的根级ID
        allDirIdArr: [], // 当前目录的所有上一级集合
        allDirId: "" // 当前目录的所有上一级集合
      }
    },
    mounted: function () {
    	let vm = this

      vm.initTable([], 1, 20, 0)
      // 信息资源分类
      vm.loadTreeData()
      // 加载提供方数据
//      vm.loadDepartmentData()
		},
    methods: {
      loadTreeData () {
        let vm = this
        dirAPIs.list({
          token: localStorage.token
        }).then(({ data }) => {
          if(data.code == 10000){
            vm.initTree(data.data)
          }
        })
      },
      loadData(params) {
        let vm = this
        dirAPIs.infoResourceList(params).then(({data}) => {
          console.log(data)
          if (data.code == 10000) {
            vm.initTable(data.data, params.pageNum, params.pageSize, data.total)
          }
        })
      },
    	initTree(dataList) {
    		let vm = this
    		vm.treeOptions = {
    			see:false,
    			data:dataList,
    			treeMethod:{
		      	append(node, data, store) {
			        vm.dialogOptions2.visible = true
              vm.addCatalogTypeFormData = {
			          node: node,
                data: data
              }
		        },
		        remove(node, data, store) {
			        vm.$confirm(`确定要删除【${data.label}】吗?`,'提示',{
			        	cancelButtonText:'取消',
			        	confirmButtonText:'确定',
			        }).then(() => {
			          dirAPIs.delete({
                  token: localStorage.token,
                  key: data.value
                }).then(({ data }) => {
			            if(data.code == 10000) {
                    vm.$message.success('删除信息资源分类成功！')

                    const parent = node.parent;
                    const children = parent.data.children || parent.data;
                    const index = children.findIndex(d => d.id === data.id);
                    children.splice(index, 1);
                  }
                  else{
                    vm.$message.error(`${data.msg}`)
                  }
                })

			        }).catch(() => {
			        	vm.$message.info('已取消');
			        })
		        },
            nodeClick(node, nodeObj, component) {
//		      	  debugger
              let pageNo = 1,
                pageSize = 20

              vm.dirId = node.value
              vm.dirTypeId = node.rootId
              vm.allDirIdArr = []
              vm.getParent(nodeObj)
              vm.allDirId = vm.allDirIdArr.reverse().join("")
//              debugger
              if(node.rootId) {
                vm.loadData({
                  token: localStorage.token,
                  pageNum: pageNo,
                  pageSize: pageSize,
                  dirId: node.value,
                  createTimeToStart: vm.createTimeToStart,
                  createTimeToEnd: vm.createTimeToEnd
                })
              }
              else{
                vm.loadData({
                  token: localStorage.token,
                  pageNum: pageNo,
                  pageSize: pageSize,
                  dirTypeId: node.value,
                  createTimeToStart: vm.createTimeToStart,
                  createTimeToEnd: vm.createTimeToEnd
                })
              }
            }
		      }
    		}
    		debugger
    	},
      getParent(parent){
        let vm = this
//        debugger
        if(parent!=null&&parent.data&&parent.data.code) {
          vm.allDirIdArr.push(parent.data.code)
          vm.getParent(parent.parent)
        }
      },
      initTable (dataList, pageNo, pageSize, total) {
        let vm = this
        let vuex = vm.$store.state.catalogManage.pages.main
        //必须先加载部门树，不然组件渲染不出来
        departmentAPIs.listTree({
          token: localStorage.token
        }).then(({ data }) => {
          if(data.code == 10000) {
            vm.deparmentData = data.data
            vm.tableOptions = {
              thead: [{
                prop: "infoResourceCode", // 数据的key值
                label: "信息资源代码" // 显示的标题
              },{
                prop: "infoResourceName", // 数据的key值
                label: "信息资源名称" // 显示的标题
              },{
                prop: "departmentName", // 数据的key值
                label: "提供方" // 显示的标题
              },{
                prop: "status", // 数据的key值
                label: "资源状态", // 显示的标题
                formatter (row, column, cellValue) {
                  switch (cellValue) {
                    case 1 :
                      return "新增未提交"
                      break
                    case 2 :
                      return "已提交待审核"
                      break
                    case 3 :
                      return "审核未通过"
                      break
                    case 4 :
                      return "审核通过已发布"
                      break
                  }
                }
              },{
                prop: "createTime", // 数据的key值
                label: "创建时间" // 显示的标题
              }],
              data: dataList,
              pageInfo: {
                currentPage: pageNo,
                pageSizes: [20, 40, 60, 80],
                pageSize: pageSize,
                layout: "prev, pager, next, total, sizes, jumper",
                total: total,
                handleSizeChange (val) {
                  vm.tableOptions.pageInfo.pageSize = val
                  vm.loadData({
                    token: localStorage.token,
                    pageNum: vm.tableOptions.pageInfo.currentPage,
                    pageSize: vm.tableOptions.pageInfo.pageSize,
                    dirId: vm.dirId,
                    resourceName: vuex.resourceName,
                    resourceCode: vuex.resourceCode,
                    departmentId: [...vuex.departmentId].pop(),
                    status: vuex.status,
                    createTimeToStart: +vuex.createTimeToStart || vm.createTimeToStart,
                    createTimeToEnd: +vuex.createTimeToEnd || vm.createTimeToEnd
                  })
                },
                handleCurrentChange (val) {
                  vm.tableOptions.pageInfo.currentPage = val
                  vm.loadData({
                    token: localStorage.token,
                    pageNum: vm.tableOptions.pageInfo.currentPage,
                    pageSize: vm.tableOptions.pageInfo.pageSize,
                    dirId: vm.dirId,
                    resourceName: vuex.resourceName,
                    resourceCode: vuex.resourceCode,
                    departmentId: [...vuex.departmentId].pop(),
                    status: vuex.status,
                    createTimeToStart: +vuex.createTimeToStart || vm.createTimeToStart,
                    createTimeToEnd: +vuex.createTimeToEnd || vm.createTimeToEnd
                  })
                }
              },
              handleSelectionChange (rows) {
                vm.tableRows = rows
              }
            }
          }
        })

      }
    }
  }
</script>
<style>
  .catalogTree{
  	background: #f1f1f1;
  }
  .catalogTree .is-current{
  	background: #fff;
  	color:#2cace2
  }
  .catalogTree .is-current button span{
  	color:#2cace2
  }
  .catalogTree .is-current button:last-child span{
  	color:#e15959
  }
  .catalogContainer{
  	width: 91%;
  	margin: 0 auto;
  }
  .catalogContainer .el-aside{
  	width: 18% !important;
  }
  .catalogContainer .el-aside .el-container{
  	width: 100%;
  	min-width: 200px;
  }
  .catalogContainer .el-main{
  	width: 81%;
  	margin-left:20px ;
  	padding: 0;
  }
  .catalogContainer .moduleBar{
  	width: 100%;
  }
  .catalogContainer .tablebar{
  	width: 100%;
  	margin-top:20px;
  }
  .failDataTable .el-table__header tr{
    background-color: #eeeeee;
  }
</style>

